package com.lolaage.tracks.test;

import java.util.List;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.lolaage.tracks.MyTrackManager;
import com.lolaage.tracks.MyTrackManager.LOC_ENGINER;
import com.lolaage.tracks.R;
import com.lolaage.tracks.Track;
import com.lolaage.tracks.TrackActivity;
import com.lolaage.tracks.TrackListener;
import com.lolaage.tracks.TrackPoint;
import com.lolaage.tracks.MyTrackManager.TRACK_STATE;

/*******************************************************
 * @author: zhaohua
 * @version: 2012-9-7
 * @see: 轨迹的显示视图
 * @Copyright: copyrights reserved by personal 2007-2012
 *******************************************************/
public class MyTracksActivity extends Activity 
implements OnClickListener,TrackListener,OnItemClickListener
{
    private Button continue_track;
    private Button start_track;
    private Button pause_track;
    private Button resume_track;
    private Button stop_track;
    private Button see_track;
    private Button switch_slef;
    private Button switch_baidu;
    private ListView listView;
    private MyTrackManager myTrackManager;
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myTrackManager = MyTrackManager.getInstance();
        initViews();
    }
    
    @Override
    protected void onResume()
    {
        super.onResume();
        loadData();
        myTrackManager.registerTrackListener(this);
    }
    
    @Override
    protected void onPause()
    {
        myTrackManager.unregisterTrackListener(this);
        super.onPause();
    }
    
    private void initViews()
    {
        continue_track = (Button)findViewById(R.id.continue_track);
        start_track = (Button)findViewById(R.id.start_track);
        pause_track = (Button)findViewById(R.id.pause_track);
        resume_track = (Button)findViewById(R.id.resume_track);
        stop_track = (Button)findViewById(R.id.stop_track);
        see_track = (Button)findViewById(R.id.see_track);
        listView = (ListView)findViewById(R.id.listView);
        switch_baidu = (Button)findViewById(R.id.switch_baidu);
        switch_slef = (Button)findViewById(R.id.switch_slef);
        
        continue_track.setOnClickListener(this);
        start_track.setOnClickListener(this);
        pause_track.setOnClickListener(this);
        resume_track.setOnClickListener(this);
        stop_track.setOnClickListener(this);
        see_track.setOnClickListener(this);
        switch_baidu.setOnClickListener(this);
        switch_slef.setOnClickListener(this);
        
        updateState(); 
        
        listView.setOnItemClickListener(this);
    }
    
    private void updateState()
    {
        TRACK_STATE state = myTrackManager.getState();
        if(state == TRACK_STATE.STARTED)
        {
            continue_track.setEnabled(false);
            start_track.setEnabled(false);
            resume_track.setEnabled(false);
            pause_track.setEnabled(true);
            stop_track.setEnabled(true);
            
            see_track.setEnabled(true);
        }else
        if(state == TRACK_STATE.PAUSEED)
        {
            continue_track.setEnabled(false);
            start_track.setEnabled(false);
            resume_track.setEnabled(true);
            pause_track.setEnabled(false);
            stop_track.setEnabled(true);
            
            see_track.setEnabled(true);
        }else
        {
            continue_track.setEnabled(true);
            start_track.setEnabled(true);
            resume_track.setEnabled(false);
            pause_track.setEnabled(false);
            stop_track.setEnabled(false);
            
            see_track.setEnabled(false);
        }
        
        LOC_ENGINER type = myTrackManager.getLocEnginerType();
        if(type == LOC_ENGINER.BAIDU)
        {
            switch_baidu.setEnabled(false);
            switch_slef.setEnabled(true);
        }else
        if(type == LOC_ENGINER.MYSLEF)
        {
            switch_baidu.setEnabled(true);
            switch_slef.setEnabled(false);
        }else
        {
            switch_baidu.setEnabled(false);
            switch_slef.setEnabled(false);
        }
    }
    
    private void loadData()
    {
        new AsyncTask<Void, Void, List<Track>>()
        {
            @Override
            protected List<Track> doInBackground(Void... params)
            {
                return myTrackManager
                .getTrackProvider().getTrackList(-1, 0, true);
            }
            
            @Override
            protected void onPostExecute(List<Track> result)
            {
                listView.setAdapter(new ArrayAdapter<Track>(MyTracksActivity.this, android.R.layout.simple_list_item_1, result));
            }           
        }.execute();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,long id)
    {
        long trackId = ((Track)parent.getItemAtPosition(position))._id;
        TrackActivity.startActivity(this, trackId);
    }
    
    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.continue_track:
            {
                myTrackManager.startContinue(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        updateState();
                    }
                });
                break;
            }
            case R.id.start_track:
            {
                myTrackManager.start(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        updateState();
                    }
                });
                break;
            }
            case R.id.pause_track:
            {
                myTrackManager.pause();
                break;
            }
            case R.id.resume_track:
            {
                myTrackManager.resume();
                break;
            }
            case R.id.stop_track:
            {
                myTrackManager.stop();
                break;
            }
            case R.id.see_track:
            {
                TrackActivity.startActivity(this);
                return;
            }
            case R.id.switch_slef:
            {
                myTrackManager.switchLocEnginer(LOC_ENGINER.MYSLEF);
                break;
            }
            case R.id.switch_baidu:
            {
                myTrackManager.switchLocEnginer(LOC_ENGINER.BAIDU);
                break;
            }
        }
        updateState();
    }

    @Override
    public void onTrackPointChanged(List<TrackPoint> appendPoints)
    {
        
    }

    @Override
    public void onNewTrack(Track track)
    {
        runOnUiThread(new Runnable()
        {
            
            @Override
            public void run()
            {
                updateState();
            }
        });
    }

    @Override
    public void onTrackUpdated(Track track)
    {
        
    }
}